<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Http\Requests;

use Gregwar\Captcha\CaptchaBuilder;

use App\admin\adminUser;

use DB;

class LoginController extends Controller
{
    //登录页面
    public function index()
    {
    	return view('admin.login');
    }

    //生成验证码的方法
    public function captch($tmp)
    {
    	//生成验证码图片的Builder对象,配置相应的属性
    	$builder = new CaptchaBuilder;

    	//设置图片宽高及字体
    	$builder->build($width = 100,$height = 40,$font = null);

    	//获取验证码的内容
    	$phrase = $builder->getPhrase();

    	//把内容放入session
    	session()->flash('mycode',$phrase);

    	//返回验证码
    	return response($builder->output())->header('content-type','image/jpeg');

    }


    public function doLogin(Request $request)
    {

    	// //获取session里边的mycode
    	$mycode = session()->get('mycode');

    	// 判断用户输入的验证码和session中的值是否相等
        if ($mycode != $request->input('code')) {
            
            return back()->with('msa','验证码错误');
        }   

    	//实例化一个模型
    	$adminUser = new adminUser();
    	
    	$ob = $adminUser->checkAdminUser($request);

    	if ($ob) {
    		session(['adminUser' => $ob]);

    		// 跳转到首页
    		return redirect('admin/index');
    	 } else {
    		
    		//返回登录页面
    		return back()->with('msg','账户或密码错误');

    	 
    	 }
	}

    //退出登录
    public function Logout(Request $request)
    {
       $request->session()->forget('adminUser');
       return redirect('admin/');
    }
}
